package com.torinosrc;

import com.sun.javafx.sg.prism.NGShape;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <b><code>TorinoMain</code></b>
 * <p/>
 * TorinoMain
 * <p/>
 * <b>Creation Time:</b> Mon Feb 14 14:52:30 CST 2017.
 *
 * @author lvxin
 * @version 0.0.1
 * @since generate 0.0.1
 */
public class TorinoMain {
    public static void main(String[] args){

        String templateName = "template/service.vm";
        String className = "Store";
        String author = "lvxin";

        Model model = new Model();
        model.setPackageLevel("service");
        model.setPackageFeature("store");

        model.setRestUrl(className.toLowerCase());
        model.setClassName(className);
        model.setClassInstanceName(className.substring(0,1).toLowerCase() + className.substring(1,className.length()));
        model.setClassDescription(className + "的具体实现");
        model.setClassCreateTime(new Date().toString());

        model.setAuthor(author);

        //System.out.println(exportJava(templateName, model));
        Write.write("D:\\Jason\\gen\\"+ model.getClassName() + "Service.java", exportJava(templateName, model));

        templateName = "template/serviceimpl.vm";
        //System.out.println(exportJava(templateName, model));
        Write.write("D:\\Jason\\gen\\"+ model.getClassName() + "ServiceImpl.java", exportJava(templateName, model));

        templateName = "template/serviceimpltest.vm";
        //System.out.println(exportJava(templateName, model));
        Write.write("D:\\Jason\\gen\\"+ model.getClassName() + "ServiceImplTest.java", exportJava(templateName, model));

        templateName = "template/dao.vm";
        model.setPackageLevel("dao");
        //System.out.println(exportJava(templateName, model));
        Write.write("D:\\Jason\\gen\\"+ model.getClassName() + "Dao.java", exportJava(templateName, model));

        templateName = "template/daoimpl.vm";
        //System.out.println(exportJava(templateName, model));
        Write.write("D:\\Jason\\gen\\"+ model.getClassName() + "DaoImpl.java", exportJava(templateName, model));

        model.setPackageLevel("model");
        model.setPackageSubLevel("entity");
        templateName = "template/model.vm";
        Write.write("D:\\Jason\\gen\\"+ model.getClassName() + ".java", exportJava(templateName, model));

        model.setPackageLevel("model");
        model.setPackageSubLevel("view");
        templateName = "template/modelview.vm";
        Write.write("D:\\Jason\\gen\\"+ model.getClassName() + "View.java", exportJava(templateName, model));

        templateName = "template/controller.vm";
        Write.write("D:\\Jason\\gen\\"+ model.getClassName() + "Controller.java", exportJava(templateName, model));


//        model.setPackageLevel("model");
//        model.setPackageSubLevel("view");
//        model.setPackageFeature("user");
//
//        model.setClassName("UserLevel");
//        model.setClassInstanceName("userLevel");
//        model.setClassDescription("UserLevel");
//        model.setClassCreateTime(new Date().toString());
//
//        List<FieldModel> fieldModels = new ArrayList<FieldModel>();
//        FieldModel fieldModel = new FieldModel();
//
//        fieldModel.setReturnType("String");
//        fieldModel.setField("levelName");
//        fieldModel.setFieldInstance("LevelName");
//
//        fieldModels.add(fieldModel);

    }

    public static String exportJava(String templateName, Model model) {
        // 创建引擎
        VelocityEngine ve = new VelocityEngine();
        // 设置模板加载路径，这里设置的是class下
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.setProperty("input.encoding", "utf-8");
        ve.setProperty("output.encoding", "utf-8");
        ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        try {
            // 进行初始化操作
            ve.init();
            // 加载模板，设定模板编码
            Template t = ve.getTemplate(templateName, "UTF-8");
            // 设置初始化数据
            VelocityContext context = new VelocityContext();

            context.put("model", model);

            // 设置输出
            StringWriter writer = new StringWriter();
            // 将环境数据转化输出
            t.merge(context, writer);

            return writer.toString();

        } catch (Exception e) {
            throw new RuntimeException("模版转化错误!");
        }
    }
}
